package com.FH.applyForm.officeServies.meeting_Type;


import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/7/15.
 * 预支费用申请单实现类
 */

@Stateless
public class meetmanagerDAOImpl extends AccessDB implements meetManagerDAO {


    @Override
    public ResultDTO getmeetManageInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();

        try {

            JSONObject json_search = new JSONObject(search);
            String sql_where = "";//条件搜索sql
            String sql_page = "";//分页sql

            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json_search.has("meet_type_id")) {
                sql_where += " AND meet_type_id ='" + json_search.getString("meet_type_id") + "'";
            }

            String sqlexit = "select count(1) from meet_type  where 1=1 ";
            String sqlexit_ok = sqlexit + sql_where + sql_page;

            String existName = getReportingToOne(sqlexit + sql_where);//查询此数据是否存在

            List<String[]> seresult = new ArrayList<String[]>();

            List<mettingManagerShowDTO> result = new ArrayList<mettingManagerShowDTO>();

            if (!existName.equals("0")) {
                String sql = "SELECT meet_type_id,meetName,meetdesc,meetvolume,meet_type_createtime,meet_device,meet_addr,meet_managermen FROM meet_type WHERE 1=1 "
                        + sql_where + " ORDER BY meet_type_createtime DESC" + sql_page;

                seresult = this.getReportingTo(sql);
                for (int i = 0; i < seresult.size(); i++) {
                    mettingManagerShowDTO mettingManagerShowDTO = new mettingManagerShowDTO();
                    mettingManagerShowDTO.setMeet_type_id(seresult.get(i)[1]);
                    mettingManagerShowDTO.setMeetName(seresult.get(i)[2]);
                    mettingManagerShowDTO.setMeetdesc(seresult.get(i)[3]);
                    mettingManagerShowDTO.setMeetvolume(seresult.get(i)[4]);
                    mettingManagerShowDTO.setMeet_type_createtime(seresult.get(i)[5]);
                    mettingManagerShowDTO.setMeetname_add_volume(seresult.get(i)[2]+"  (可容纳"+seresult.get(i)[4]+"人)");
                    mettingManagerShowDTO.setMeet_device(seresult.get(i)[6]);
                    mettingManagerShowDTO.setMeet_addr(seresult.get(i)[7]);
                    mettingManagerShowDTO.setMeet_managermen(seresult.get(i)[8]);
                    result.add(mettingManagerShowDTO);
                }
            }

            //如果查询成功，构建返回对象

            if (result.size() > 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

            resultDTO.setStr(result.size()+"");
            resultDTO.setValues(result);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultDTO;
    }

    @Override
    public ResultDTO addmeetManageInfo(mettingManagerPullDTO dataDTO) {
        String sql_isHas = "select count(1) from meet_type WHERE meetName='" + dataDTO.getMeetName() + "'";

        String exist = getReportingToOne(sql_isHas);//插入前，查询是否有重复字段

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式化

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

        int resultls = 0;

        //构建返回对象
        ResultDTO resultDTO = new ResultDTO();

        if (exist.equals("0")) {

            String meet_type_insert = "";


            //构建插入sql
            meet_type_insert += "INSERT  INTO meet_type(meet_type_id,meetName,meetdesc,meetvolume,meet_type_createtime,meet_device,meet_addr) " +
                    "VALUES('" + uuid + "','" + dataDTO.getMeetName() + "','" + dataDTO.getMeetdesc() + "','"
                    + dataDTO.getMeetvolume() + "','" + df.format(new Date()) + "','" + dataDTO.getMeet_device() + "','" + dataDTO.getMeet_addr() + "')";
            //插入业务表
            resultls = addReportingTo(meet_type_insert);

        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("会议信息已存在");
            return resultDTO;
        }
        if (resultls != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }


        return resultDTO;
    }

    @Override
    public ResultDTO updatemeetManageInfo(mettingManagerShowDTO dataDTO) {
        return null;
    }

    @Override
    public ResultDTO deletemeetManageInfo(mettingManagerShowDTO dataDTO) {

        //删除之前首先查询该表中是否有记录

        String sql="select count(1) from meet_type WHERE meet_type_id='"+dataDTO.getMeet_type_id()+"'";
        String exist= getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if(!exist.equals("0")){
            String QUERY = "";
            QUERY += "delete from meet_type where meet_type_id = '"+dataDTO.getMeet_type_id()+"'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result!= 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;

    }
}
